/**********************************
|   Author	: XForce              |
|								  |
|                                 |
***********************************/

//#include <vector>
//#include <string>
//#include <sstream>
//#include <d3d9.h>
//#include <d3dx9.h>
#include <psapi.h>
//#include "drawfuncs.h"
//#pragma comment(lib, "d3d9.lib")
//#pragma comment(lib , "d3dx9.lib")
//extern ID3DXFont *xfont;

#undef SAFE_RELEASE
#define SAFE_RELEASE( d ) \
	if ( d ) \
	{ \
		d->Release(); \
		d = NULL; \
	}

//extern LPD3DXLINE g_pLine;

enum types
{
	Window,
	Button,
	Textbox,
	ListBox,
	ComboBox,
	NumericUpDown,
};


enum ControlTypes
{
	Buttons,
	Textboxs,
	ListBoxs,
	NumericUpDowns,
};


enum WindowState
{
	Hide,
	Normal,
	Active,
};


struct XListBoxItem
{
	std::string name;
	std::string info;
	int selected;
};

class XControl
{
public: 
	int x;
	int y;
	int h;
	int w;
	POINT* window;
	virtual void Render(LPDIRECT3DDEVICE9 Device);
	std::string title;
	types type;
	int id;
	int active;
	
	void (*clickevent)(XControl* sender,types type); //Only for buttons
};

class XTextBox : public XControl
{
public:
	XTextBox();
	XTextBox(int x,int y,int h,int w,int id);
	std::string getText();
	//void Render();
	int caretPos;
	int caretPosScreen; 
	int caretTick;
	int showPosbegin; 
	int showPosend; 
	int charw; 
	bool showCaret; 
	DWORD ltick; 
	std::string textbuffer;
	void HandleInput(int nCode, WPARAM wParam, LPARAM lParam);
	void Render(LPDIRECT3DDEVICE9 Device);
};


class XButton : public XControl
{

public:
	XButton();
	XButton(int x,int y,int h,int w, std::string title,void (*func)(XButton* sender),int id);
	XButton(RECT rct,std::string title,void* func,int id);
	

	void Render(LPDIRECT3DDEVICE9 Device);
	void(*clickevent)(XButton* sender);
	int getstate(); // 1 - Normal  2 - Hide 3 - Closed
	void Release();
};

class XNumericUpDown : public XControl
{

public:
	XNumericUpDown();
	XNumericUpDown(int x,int y,int h,int w, std::string title,int id);
	XNumericUpDown(RECT rct,std::string title,void* func,int id);
	

	void Render(LPDIRECT3DDEVICE9 Device);
	int mouse;
	float step;
	float value;
	float max;
	float min;
	int getstate(); // 1 - Normal  2 - Hide 3 - Closed
	void Release();
};



class XListBox : public XControl
{
public:
	XListBox();
	XListBox(int x,int y,int h,int w, std::string title,int id);
	int selected;
	std::vector<XListBoxItem> items;
	int out;
	int slidermouse;
	int slidery;
	void AddItem(XListBoxItem item);
	void RemoveItem(XListBoxItem item);
	void Render(LPDIRECT3DDEVICE9 Device);
	XListBoxItem GetSelectedItem();
};


class Control
{
public:
	Control();
	Control(ControlTypes type,int x, int y,int h,int w,std::string title);
	Control(int x,int y,int h,int w, std::string title,void (*func)(Control* sender)); // Button
	void Render(LPDIRECT3DDEVICE9 Device);
	ControlTypes type;
	POINT* window;
#pragma region ListBox
	int AddItem(XListBoxItem item); // Returns -2 if its not a Listbox
	int RemoveItem(XListBoxItem item); // Returns -2 is its not a Listbox
	int GetSelectedItem(XListBoxItem* item); // Returns -2 if its not a Listbox

	int selected; // Only used by ListBox
	std::vector<XListBoxItem> items; //Only used by ListBox
	int out;
	int slidermouse;
	int slidery;


	int x;
	int y;
	int h;
	int w;

#pragma endregion

#pragma region Button
	void(*clickevent)(Control* sender);
#pragma endregion

	#pragma region NumericUpDown
	int mouse;
	float step;
	float value;
	float max;
	float min;
	#pragma endregion

#pragma region TextBox
	void HandleInput(int nCode, WPARAM wParam, LPARAM lParam);

	int caretPos;
	int caretPosScreen; 
	int caretTick;
	int showPosbegin; 
	int showPosend; 
	int charw; 
	bool showCaret; 
	DWORD ltick; 
	std::string textbuffer;
#pragma endregion
};

class XWindow
{

public:
	int x,y,h,w;
	std::string title;
	XWindow();
	XWindow(int x,int y,int h,int w, std::string title,int id);
	XWindow(RECT rct,std::string title ,int id);
	D3DCOLOR textcolor;
	int id;
	POINT position;
	bool move;
	WindowState state;
	bool fadeout;
	std::vector<Control*> conts;
	std::vector<XControl*> controls;
	void AddControl(XControl* control);
	void RemoveControl(XControl* control);
	void Render(LPDIRECT3DDEVICE9 Device);
	int getstate(); // 1 - Normal  2 - Hide 3 - Closed
	void Release();
};



class XGUI
{
	public:
	XGUI();
	std::vector<XWindow*> windows;
	void AddWindow(XWindow* window);
	void RemoveWindow(XWindow* window);
	void Release();
	bool disabled;
	static DWORD WINAPI hooks(LPVOID lpParm);
	//static LRESULT CALLBACK KeyboardHookProc (int nCode, WPARAM wParam, LPARAM lParam);
	//static LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam);
	void Render(LPDIRECT3DDEVICE9 pDevice/*,D3DVIEWPORT9 viewport,ID3DXFont* xfont*/);
};


